<?php
/**
 * 表单列挂件
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/16 0016
 * Time: 14:46
 */

namespace app\common\widget;
use app\common\library\CommonFun;

/**
 * Class WidgetEditFormColumn
 * @package app\common\widget
 * @method WidgetEditFormColumn setWidthColXs($value) 设置Xs(min-width:0px)宽度值,$value=1到12
 * @method WidgetEditFormColumn setWidthColSm($value) 设置Sm(min-width:768px)宽度值,$value=1到12
 * @method WidgetEditFormColumn setWidthColMd($value) 设置Md(min-width:992px)宽度值,$value=1到12
 * @method WidgetEditFormColumn setWidthColLg($value) 设置Lg(min-width:1200px)宽度值,$value=1到12
 * @method WidgetInput addTextInput($sort=0)
 * @method WidgetSelect addSelectInput($sort=0)
 * @method WidgetCheckBox addCheckBoxInput($sort=0)
 * @method WidgetRadio addRadioInput($sort=0)
 * @method WidgetTextArea addTextAreaInput($sort=0)
 * @method WidgetCKeditor addCKeditorInput($sort=0)
 * @method WidgetButton addButton($sort=0)
 *
 */
class WidgetEditFormColumn extends WidgetCommon
{

    /**
     * 宽度列表
     * @var array
     */
    private $widthList = [
        'xs'=>WidgetConst::colxs6,
        'sm'=>WidgetConst::colsm6,
        'md'=>WidgetConst::colmd6,
        'lg'=>WidgetConst::collg6,
    ];

    /**
     * 表单元素列表
     * @var array
     */
    private $inputList = [];

    /**
     * 搜索表单按钮组
     * @var array
     */
    private $buttonList = [];

    /**
     * 构造方法, 渲染挂件
     * @return mixed
     * @throws \think\Exception
     */
    function render()
    {
        // Before Render Process
        $this->beforeRender();

        // 设置宽度class
        $this->setClass( $this->widthList );

        // 分配元素列表
        if ( CommonFun::array_is_assoc( $this->inputList ) === false ) ksort( $this->inputList );
        self::$view->assign('inputList', $this->inputList);

        // 按钮元素
        if ( $this->buttonList && CommonFun::array_is_assoc( $this->buttonList ) === false ) ksort( $this->buttonList );
        self::$view->assign('buttonList', $this->buttonList);

        return $this->fetch();
    }

    /**
     * Before Render
     * @return $this
     * @throws \think\Exception
     */
    private function beforeRender() {
        // 检测
        if ( !$this->inputList ) $this->throwException("表单列没有From元素");

        // 生成input html 列表
        foreach ( $this->inputList as &$v ) {
            if ( $v instanceof WidgetInput) {
                $widthList = $v->getWidth();
                $widthList = implode(" ", $widthList);
                $inputHtml =  $v->render();
                $inputID = $v->getId();
                $inputTitle = $v->getTitle();
                $ele = "<div class=\"col-sm-12 col-lg-12 col-xs-12 col-md-12\" style=\"padding:0;\"><div class=\"row {$widthList} form-group\"><label for=\"{$inputID}\" class=\"control-label \">{$inputTitle}</label>{$inputHtml}</div></div>";
                $v = $ele;
            }
        }

        // Render Button
        if ( $this->buttonList ) {
            foreach ( $this->buttonList as &$b ) {
                if ( $b instanceof WidgetButton) $b = $b->render();
            }
        }

        return $this;
    }

    /**
     * 初始化函数
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置默认宽度
        foreach ( $this->widthList as $media=>$v ) {
            $this->setWidth( $media, $v );
        }
    }

    /**
     * 设置宽度
     * @param $media
     * @param $width
     * @return $this
     */
    private function setWidth( $media, $width ) {
        $this->widthList[$media] = $width;
        return $this;
    }

    /**
     * 设置一个表单元素
     * @param WidgetInput $input
     * @param int $sort
     * @return $this
     */
    private function setInput( WidgetInput &$input, $sort=0) {
        if ( $sort ) {
            $this->inputList[(int)$sort] = $input;
        }else{
            $this->inputList[] = $input;
        }

        return $this;
    }

    /**
     * 设置一个按钮
     * @param WidgetButton $button
     * @param int $sort
     * @return $this
     */
    private function setButton( WidgetButton &$button, $sort=0 ) {
        if ( $sort ) {
            $this->buttonList[(int)$sort] = $button;
        }else{
            $this->buttonList[] = $button;
        }
        return $this;
    }

    /**
     * __call 魔术方法
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \think\Exception
     */
    public function __call($name, $arguments)
    {
        // 匹配宽度
        if ( strpos( $name, 'setWidthCol' ) !==false ) {
            $style = str_replace( "setWidth", "", $name );
            if ( $arguments[0]<1 || $arguments[0]>12 ) $this->throwException( "编辑表单宽度阈值错误" );
            $const = strtolower($style).$arguments[0];
            $width = WidgetConst::getConstant($const);
            // 匹配出media
            $pattern = "/^col([A-Za-z]*)[1-9]*/";
            preg_match($pattern,$const,$match);
            $media = $match[1];
            return $this->setWidth( $media, $width );
        }

        // 匹配添加Input
        if ( $res = preg_match("/^add([A-Za-z]*)Input/",$name,$match) ) {
            $type = $match[1];
            if ( $type == "Text" ) {
                $obj = new WidgetInput();
            }else{
                $class_name = 'Widget'.$type;
                $class = __NAMESPACE__."\\{$class_name}";
                if ( !class_exists($class) ) $this->throwException( "编辑表单Input元素不存在" );
                $obj = new $class();
            }
            $sort = isset($arguments[0]) ? $arguments[0] : 0;
            $this->setInput( $obj, $sort );
            return $obj;
        }

        // add Button
        if ( $name == "addButton" ) {
            $buttonObj = new WidgetButton();
            $sort = isset($arguments[0]) ? $arguments[0] : 0;
            $this->setButton($buttonObj, $sort);
            return $buttonObj;
        }
        return $this;
    }



}